package com.warrior.superdata.service.impl;

import com.warrior.superdata.enums.DatasourceType;
import com.warrior.superdata.enums.OperateType;
import com.warrior.superdata.service.SqlService;
import com.warrior.superdata.exceptionhandler.ApplicationException;
import com.warrior.superdata.mysql.entity.dto.ApiInfoDetailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @ClassName: SqlServiceImpl.java
 * @date: 2022/3/10 17:40
 * @author xujian
 */
@Service
public class SqlServiceImpl {

    Map<String, SqlService> sqlServiceMap = new HashMap<>();

    @Autowired
    public SqlServiceImpl(List<SqlService> executeSqlServiceList) {
        executeSqlServiceList.forEach(item -> sqlServiceMap.put(item.getOperateType() + item.getDatasourceType(), item));
    }

    public Object execute(ApiInfoDetailDto apiInfoDetailDto) throws ApplicationException {
        return sqlServiceMap.get(OperateType.getNameByType(apiInfoDetailDto.getOperateType()) +
                DatasourceType.getNameByType(apiInfoDetailDto.getDatasourceType())).execute(apiInfoDetailDto);
    }

}
